import { createApp } from 'vue'
import App from './App'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import request from './utils/request'
import api from './api'
import '@fortawesome/fontawesome-free/css/all.css'

const app = createApp(App)

app.config.errorHandler = (err) => {
  
  if (err.message.includes('ResizeObserver loop') || 
      err.message.includes('cancel')) {
    return;
  }
  console.error(err);
};

if (process.env.NODE_ENV === 'development') {
  app.config.warnHandler = () => null;
}

const debounce = (fn, delay) => {
  let timer = null;
  return function() {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.apply(this, arguments);
    }, delay);
  };
};

const handleResizeError = debounce(() => {
  const overlay = document.getElementById('webpack-dev-server-client-overlay');
  if (overlay) {
    overlay.remove();
  }
}, 100);

window.addEventListener('error', (event) => {
  if (event.message.includes('ResizeObserver loop')) {
    event.stopPropagation();
    event.preventDefault();
    handleResizeError();
  }
}, true);

const originalError = console.error;
console.error = (...args) => {
  if (
    args[0] && 
    typeof args[0] === 'object' && 
    args[0].message && 
    args[0].message.includes('ResizeObserver loop')
  ) {
    return;
  }
  originalError.apply(console, args);
};

app.use(router)
app.use(ElementPlus, {
  locale: zhCn,
})

app.config.globalProperties.$http = request

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.config.globalProperties.$api = api
app.mount('#app')

// 添加以下代码来忽略特定错误
const ignoreWarnMessage = 'ResizeObserver loop completed with undelivered notifications.';
const originalConsoleError = console.error;
console.error = (...args) => {
  if (args[0] === ignoreWarnMessage) {
    return;
  }
  originalConsoleError.apply(console, args);
};